﻿package mortal.game.view.common
{
    import Framework.Util.*;
    import com.gengine.global.*;
    import com.greensock.*;
    import com.greensock.easing.*;
    import com.mui.controls.*;
    import com.mui.core.*;
    import com.mui.display.*;
    import com.mui.manager.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.text.*;
    import flash.utils.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.component.gconst.*;

    public class BaseProgressBar extends ToolTipSprite
    {
        protected var _bgClass:String;
        protected var _progressBarBg:Bitmap;
        protected var _tweenProgressBarClass:String;
        protected var _tweenProgcessScaleBmp:ScaleBitmap;
        public var _tweenProgressBar:Bitmap;
        protected var _tween:TweenMax;
        protected var _progressBarClass:String;
        protected var _progcessScaleBmp:ScaleBitmap;
        public var _progressBar:Bitmap;
        protected var _label:GTextFiled;
        protected var _value:Number = 0;
        protected var _tweenValue:Number = 0;
        protected var _totalValue:Number = 0;
        protected var _lastWidth:int = 0;
        protected var _progressWidth:Number;
        protected var _progressHeight:Number;
        protected var _labelType:int;
        protected var _replaceTxt:String;
        public var _isOverShowValue:Boolean;
        private var _isCacheBmp:Boolean = false;
        public static var ProgressBarTextNone:int = 0;
        public static var ProgressBarTextNumber:int = 1;
        public static var ProgressBarTextPercent:int = 2;
        public static var ProgressBarTextToolTip:int = 4;
        public static var ProgressBarTextOver:int = 8;
        public static var ProgressBarTextOnlyShowValue:int = 16;
        private static var cacheBmpDic:Dictionary = new Dictionary();
        private static const connect:String = "_";

        public function BaseProgressBar()
        {
            mouseEnabled = false;
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._tweenProgressBar = UIFactory.bitmap("");
            this.addChild(this._tweenProgressBar);
            this._progressBar = UIFactory.bitmap("");
            this.addChild(this._progressBar);
            this._label = UIFactory.textField("", 0, 0, 200, 20, this, GlobalStyle.textFormatPutong);
            this._label.autoSize = TextFieldAutoSize.CENTER;
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            UIFactory.disposeBitmap(this._progressBarBg);
            this._label.dispose(param1);
            UIFactory.disposeBitmap(this._progressBar, param1);
            this._value = 0;
            this._totalValue = 0;
            this._progressWidth = 0;
            this._progressHeight = 0;
            this._lastWidth = 0;
            this._labelType = 1;
            this._replaceTxt = "";
            this._bgClass = null;
            this._progressBarBg = null;
            this._progcessScaleBmp = null;
            this._progressBarClass = null;
            this._progressBar = null;
            this._label = null;
            this._isCacheBmp = false;
            this._tweenValue = 0;
            UIFactory.disposeBitmap(this._tweenProgressBar, param1);
            this._tweenProgcessScaleBmp = null;
            this._tweenProgressBarClass = null;
            this._tweenProgressBar = null;
            if (this._tween)
            {
                this._tween.kill();
                this._tween = null;
            }
            this._isOverShowValue = false;
            super.disposeImpl(param1);
            return;
        }// end function

        public function setBg(param1:String, param2:Boolean, param3:int = 0, param4:int = 0, param5:Boolean = true) : void
        {
            if (this._progressBarBg)
            {
                UIFactory.disposeBitmap(this._progressBarBg);
                this._progressBarBg = null;
            }
            if (param2)
            {
                this._progressBarBg = ResourceConst.getScaleBitmap(param1);
                this._progressBarBg.width = param3;
                this._progressBarBg.height = param4;
            }
            else
            {
                this._progressBarBg = UIFactory.bitmap(param1);
            }
            if (param5)
            {
                this.addChildAt(this._progressBarBg, 0);
            }
            return;
        }// end function

        public function setProgress(param1:String, param2:Boolean = true, param3:Number = 0, param4:Number = 0, param5:int = 0, param6:int = 0) : void
        {
            this._progressBarClass = param1;
            if (this._progcessScaleBmp)
            {
                this._progcessScaleBmp.dispose();
                this._progcessScaleBmp = null;
            }
            if (param2)
            {
                this._progcessScaleBmp = ResourceConst.getScaleBitmap(param1);
            }
            this._progressBar.x = param3;
            this._progressBar.y = param4;
            this._progressWidth = param5;
            this._progressHeight = param6;
            return;
        }// end function

        public function setTweenProgress(param1:String, param2:Boolean = true, param3:Number = 0, param4:Number = 0, param5:int = 0, param6:int = 0) : void
        {
            this._tweenProgressBarClass = param1;
            if (this._tweenProgcessScaleBmp)
            {
                this._tweenProgcessScaleBmp.dispose();
                this._tweenProgcessScaleBmp = null;
            }
            if (param2)
            {
                this._tweenProgcessScaleBmp = ResourceConst.getScaleBitmap(param1);
            }
            this._tweenProgressBar.x = param3;
            this._tweenProgressBar.y = param4;
            return;
        }// end function

        public function setLabel(param1:int = 1, param2:Number = 0, param3:Number = 0, param4:Number = 200, param5:Number = 20, param6:GTextFormat = null, param7:String = "") : void
        {
            this._labelType = param1;
            this._replaceTxt = param7;
            this._label.x = param2;
            this._label.y = param3;
            this._label.width = param4;
            this._label.height = param5;
            if (param6)
            {
                this._label.defaultTextFormat = param6;
            }
            if (this._labelType == ProgressBarTextOver)
            {
                this._label.visible = false;
                this.configEventListener(MouseEvent.ROLL_OUT, this.showText);
                this.configEventListener(MouseEvent.ROLL_OVER, this.showText);
            }
            return;
        }// end function

        public function setLabelPos(param1:int, param2:int) : void
        {
            this._label.x = param1;
            this._label.y = param2;
            return;
        }// end function

        public function set isOverShowValue(param1:Boolean) : void
        {
            this._isOverShowValue = param1;
            if (this._isOverShowValue && this._labelType == ProgressBarTextPercent)
            {
                this.configEventListener(MouseEvent.ROLL_OUT, this.showValueText);
                this.configEventListener(MouseEvent.ROLL_OVER, this.showValueText);
            }
            else
            {
                this.removeEventListener(MouseEvent.ROLL_OUT, this.showValueText);
                this.removeEventListener(MouseEvent.ROLL_OVER, this.showValueText);
            }
            return;
        }// end function

        private function showValueText(event:MouseEvent) : void
        {
            var _loc_2:String = null;
            if (event.type == MouseEvent.ROLL_OUT)
            {
                _loc_2 = int(this._value / this._totalValue * 100) + "%";
            }
            else if (event.type == MouseEvent.ROLL_OVER)
            {
                _loc_2 = this._value + "/" + this._totalValue;
            }
            if (this._replaceTxt)
            {
                _loc_2 = this._replaceTxt.replace("{0}", _loc_2);
            }
            this._label.text = _loc_2;
            return;
        }// end function

        private function showText(event:MouseEvent) : void
        {
            if (event.type == MouseEvent.ROLL_OUT)
            {
                this._label.visible = false;
            }
            else if (event.type == MouseEvent.ROLL_OVER)
            {
                this._label.visible = true;
            }
            return;
        }// end function

        private function killTween() : void
        {
            if (this._tween)
            {
                this._tween.kill();
                this._tween = null;
            }
            return;
        }// end function

        public function setValue(param1:Number, param2:Number, param3:Boolean = false) : void
        {
            if (param1 == this._value && this._totalValue == param2)
            {
                return;
            }
            var _loc_4:* = this._totalValue == 0;
            this._tweenValue = this._value;
            this._value = param1;
            this._totalValue = param2;
            if (_loc_4 || !this._tweenProgressBarClass || this._tweenValue > param1 && !param3)
            {
                this.drawBar();
            }
            this.dispatchEvent(new Event(Event.CHANGE));
            if (this._tweenProgressBarClass)
            {
                this.killTween();
                if (this._tweenValue > param1 && param3)
                {
                    this._tween = TweenMax.to(this, 0.8, {frameInterval:1, tweenValue:this._totalValue, onComplete:this.tweenNext});
                }
                else
                {
                    this._tween = TweenMax.to(this, 0.8, {frameInterval:1, tweenValue:param1, onComplete:this.tweenCompl, ease:Quint.easeOut});
                }
            }
            return;
        }// end function

        public function tweenNext() : void
        {
            this.drawPerBmp(0);
            this.tweenValue = 0;
            if (this._value)
            {
                this.killTween();
                this._tween = TweenMax.to(this, 0.4, {frameInterval:1, tweenValue:this._value, onComplete:this.tweenCompl, ease:Quint.easeOut});
            }
            return;
        }// end function

        public function tweenCompl() : void
        {
            this.drawBar();
            return;
        }// end function

        public function set tweenValue(param1:Number) : void
        {
            var cacheBmp:BitmapData;
            var bitmap:BitmapData;
            var perBitmapData:BitmapData;
            var value:* = param1;
            if (this.tweenValue > this._totalValue)
            {
                return;
            }
            this._tweenValue = value;
            var per:* = this._tweenValue / this._totalValue;
            var intPer:* = int(per * 100);
            per = intPer / 100;
            var width:* = per * this._progressWidth;
            if (this._isCacheBmp)
            {
                cacheBmp = getCacheBmp(this._tweenProgressBarClass, width);
                if (cacheBmp)
                {
                    this._tweenProgressBar.bitmapData = cacheBmp;
                    return;
                }
            }
            if (this._tweenProgressBar && this._tweenProgressBar.bitmapData)
            {
                if (!this._isCacheBmp)
                {
                    this._tweenProgressBar.bitmapData.dispose();
                }
                this._tweenProgressBar.bitmapData = null;
            }
            try
            {
                if (width > 0 && this._progressHeight > 0 && this._tweenProgressBarClass)
                {
                    bitmap = GlobalClass.getBitmapData(this._tweenProgressBarClass);
                    if (this._tweenProgcessScaleBmp && bitmap.width - this._tweenProgcessScaleBmp.scale9Grid.width < width)
                    {
                        this._tweenProgcessScaleBmp.setSize(width, bitmap.height);
                        this._tweenProgressBar.bitmapData = this._tweenProgcessScaleBmp.bitmapData;
                    }
                    else
                    {
                        perBitmapData = new BitmapData(width, this._progressHeight, true, 16777215);
                        perBitmapData.draw(bitmap, new Matrix(), null, null, new Rectangle(0, 0, width, this._progressHeight));
                        this._tweenProgressBar.bitmapData = perBitmapData;
                    }
                    if (this._isCacheBmp)
                    {
                        setCacheBmp(this._tweenProgressBarClass, width, this._tweenProgressBar.bitmapData);
                    }
                }
            }
            catch (e:Error)
            {
                if (Global.isDebugModle)
                {
                    throw new Exception("BaseProgressBar tweenValue报错" + _bgClass + "," + _progressBarClass + "," + _tweenProgressBarClass);
                }
            }
            return;
        }// end function

        public function get tweenValue() : Number
        {
            return this._tweenValue;
        }// end function

        public function drawBar() : void
        {
            var _loc_1:String = "";
            if (this.value > this.totalValue)
            {
                this._value = this.totalValue;
            }
            switch(this._labelType)
            {
                case BaseProgressBar.ProgressBarTextNone:
                {
                    _loc_1 = "";
                    this._label.visible = false;
                    break;
                }
                case BaseProgressBar.ProgressBarTextNumber:
                {
                    _loc_1 = this._value + "/" + this._totalValue;
                    this._label.visible = true;
                    break;
                }
                case BaseProgressBar.ProgressBarTextPercent:
                {
                    _loc_1 = Math.ceil(this._value / this._totalValue * 100) + "%";
                    this._label.visible = true;
                    break;
                }
                case BaseProgressBar.ProgressBarTextToolTip:
                {
                    _loc_1 = "";
                    this._label.visible = false;
                    this.mouseEnabled = true;
                    this.toolTipData = this._value + "/" + this._totalValue;
                    break;
                }
                case BaseProgressBar.ProgressBarTextOver:
                {
                    this.mouseEnabled = true;
                    _loc_1 = this._value + "/" + this._totalValue;
                    break;
                }
                case BaseProgressBar.ProgressBarTextOnlyShowValue:
                {
                    this.visible = true;
                    _loc_1 = this._value.toString();
                    break;
                }
                default:
                {
                    break;
                }
            }
            if (this._replaceTxt)
            {
                _loc_1 = this._replaceTxt.replace("{0}", _loc_1);
            }
            this._label.text = _loc_1;
            this.drawPerBmp(this._value / this._totalValue);
            return;
        }// end function

        private function drawPerBmp(param1:Number) : void
        {
            var _loc_4:BitmapData = null;
            var _loc_5:BitmapData = null;
            var _loc_6:BitmapData = null;
            var _loc_2:* = int(param1 * 100);
            param1 = _loc_2 / 100;
            var _loc_3:* = param1 * this._progressWidth;
            if (this._lastWidth == _loc_3)
            {
                return;
            }
            this._lastWidth = _loc_3;
            if (this._isCacheBmp)
            {
                _loc_4 = getCacheBmp(this._progressBarClass, _loc_3);
                if (_loc_4)
                {
                    this._progressBar.bitmapData = _loc_4;
                    return;
                }
            }
            if (this._progressBar && this._progressBar.bitmapData)
            {
                if (!this._isCacheBmp)
                {
                    this._progressBar.bitmapData.dispose();
                }
                this._progressBar.bitmapData = null;
            }
            if (_loc_3 > 0 && this._progressBarClass)
            {
                _loc_5 = GlobalClass.getBitmapData(this._progressBarClass);
                if (this._progcessScaleBmp && _loc_5.width - this._progcessScaleBmp.scale9Grid.width < _loc_3)
                {
                    this._progcessScaleBmp.setSize(_loc_3, _loc_5.height);
                    this._progressBar.bitmapData = this._progcessScaleBmp.bitmapData;
                }
                else
                {
                    _loc_6 = new BitmapData(_loc_3, this._progressHeight, true, 16777215);
                    _loc_6.draw(_loc_5, new Matrix(), null, null, new Rectangle(0, 0, _loc_3, this._progressHeight));
                    this._progressBar.bitmapData = _loc_6;
                }
                if (this._isCacheBmp)
                {
                    setCacheBmp(this._progressBarClass, _loc_3, this._progressBar.bitmapData);
                }
            }
            return;
        }// end function

        public function get value() : Number
        {
            return this._value;
        }// end function

        public function set value(param1:Number) : void
        {
            this._value = param1;
            this.drawBar();
            return;
        }// end function

        public function set totalValue(param1:Number) : void
        {
            this._totalValue = param1;
            this.drawBar();
            return;
        }// end function

        public function get totalValue() : Number
        {
            return this._totalValue;
        }// end function

        public function get lastWidth() : int
        {
            return this._lastWidth;
        }// end function

        public function get progressBar() : Bitmap
        {
            return this._progressBar;
        }// end function

        public function get isCacheBmp() : Boolean
        {
            return this._isCacheBmp;
        }// end function

        public function set isCacheBmp(param1:Boolean) : void
        {
            this._isCacheBmp = param1;
            return;
        }// end function

        private static function getCacheBmp(param1:String, param2:int) : BitmapData
        {
            var _loc_3:* = param1 + connect + param2;
            return cacheBmpDic[_loc_3];
        }// end function

        private static function setCacheBmp(param1:String, param2:int, param3:BitmapData) : void
        {
            var _loc_4:* = param1 + connect + param2;
            cacheBmpDic[_loc_4] = param3;
            return;
        }// end function

    }
}
